package com.ztjava.common.controller;

import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;
import com.ztjava.common.model.tree.TreeInfo;
import com.ztjava.common.model.tree.TreeItem;
import com.ztjava.common.service.TreeService;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.PortalClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;

@ControllerClass(path="/common/tree",client=PortalClient.class)
@UserController(portal=Constant.P_PORTAL)
public class TreePortalController extends BaseController{
	private static TreeService treeService = Duang.duang(TreeService.class);
	/*************
	 *修改Info
	**************/
	@UserMethod("CommonTreeInfo")
	public void addEditInfo(){
		Integer id = getParaToInt("id",0);
		TreeInfo treeInfo = new TreeInfo();
		if(id>0){
			treeInfo = TreeInfo.dao.findById(id);
		}
		setAttr("treeInfo",treeInfo);
		render("addEditInfo.html");
	}

	/*************
	 *保存Info
	**************/
	@UserMethod("CommonTreeInfo")
	public void saveInfo(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		TreeInfo treeInfo = getModel(TreeInfo.class);
		Ret ret = treeService.checkRepeat(treeInfo,TreeInfo.tablename
			,""		,new String[]{"code","编码不能重复！"});
		if(ret.get("result").equals("error")){
			renderError(ret.get(Constant.MESSAGE).toString());
			return;
		}
		saveModel(treeInfo,authSession.getId());
		
		treeService.loadInfoCache(treeInfo.getInt("id"));
		renderSuccess();
	}

	/*************
	 *Info列表
	**************/
	@UserMethod("CommonTreeInfo")
	public void infoList(){
		
		if(!getPara("is_ajax","F").equals("T")){
			render("infoList.html");
			return;
		}
		renderPageJson(treeService.infoPage(client, getParaMap()));
	}

	/*************
	 *批量删除Info
	**************/
	@UserMethod("CommonTreeInfo")
	public void batchDeleteInfo(){
		String[] btSelectItem = getParaValues("btSelectItem");
		treeService.batchDelete(TreeInfo.tablename, btSelectItem);
		renderSuccess();
	}

	/*************
	 *修改Item
	**************/
	@UserMethod("CommonTreeInfo")
	public void addEditItem(){
		Integer id = getParaToInt("id",0);
		TreeItem treeItem = new TreeItem();
		if(id>0){
			treeItem = TreeItem.dao.findById(id);
			if(treeItem.getInt("parent_id")>0){
					TreeItem parentItem = TreeItem.dao.findById(treeItem.getInt("parent_id"));
					setAttr("parentItem", parentItem);
			}
		}else{
				Integer parent_id = getParaToInt("parent_id",0);
				treeItem.set("parent_id", parent_id);
				if(parent_id>0){
					TreeItem parentItem = TreeItem.dao.findById(parent_id);
					setAttr("parentItem", parentItem);
				}
				treeItem.set("info_id",getParaToInt("info_id",0));
		}
		setAttr("treeItem",treeItem);
		render("addEditItem.html");
	}

	/*************
	 *保存Item
	**************/
	@UserMethod("CommonTreeInfo")
	public void saveItem(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		TreeItem treeItem = getModel(TreeItem.class);
		saveModel(treeItem,authSession.getId());
			if(treeItem.getInt("parent_id")>0){
				TreeItem parent = TreeItem.dao.findById(treeItem.getInt("parent_id"));
				treeItem.set("num", parent.getStr("num")+treeItem.getInt("id")+",");
			}else {
				treeItem.set("num", treeItem.getInt("id")+",");
			}
			treeItem.update();
			setAttr(Constant.RESULT, Constant.SUCCESS);
			setAttr(Constant.MESSAGE, "保存成功！");
			setAttr("object", treeItem);
			
			treeService.loadInfoCache(treeItem.getInt("info_id"));
			renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"object"});
	}

	/*************
	 *Item列表
	**************/
	@UserMethod("CommonTreeInfo")
	public void itemList(){
		Integer info_id = getParaToInt("info_id");
		setAttr("info_id",info_id);
		List<TreeItem> list = TreeItem.dao.find("select id,parent_id as pId,name from "+TreeItem.tablename+" where info_id="+info_id
				+" order by sort asc,id asc");
		setAttr("listJSON", JsonKit.toJson(list));
		render("itemList.html");
	}

	/*************
	 *删除Item
	**************/
	@UserMethod("CommonTreeInfo")
	public void deleteItem(){
		Integer id = getParaToInt("id",0);
		Integer info_id = getParaToInt("info_id");
		TreeItem child = TreeItem.dao.findFirst("select id from "+TreeItem.tablename+" where parent_id=?",id);
		if(child!=null){
			renderError("存在子分类不允许删除！");
			return;
		}
		TreeItem.dao.deleteById(id);
		setAttr(Constant.RESULT, Constant.SUCCESS);
		setAttr(Constant.MESSAGE, "删除成功！");
		setAttr("id", id);
		treeService.loadInfoCache(info_id);
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"id"});
	}
}
